<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #007BFF;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            flex-direction: column;
        }
        .container {
            background: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            max-width: 400px; /* 调整宽度 */
            width: 100%;
            text-align: center;
        }
        h1 {
            margin-bottom: 20px;
            color: white;
        }
        h2 {
            margin-bottom: 20px;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        label {
            margin-bottom: 5px;
            text-align: left;
        }
        input {
            margin-bottom: 10px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .remember-me {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }
        button {
            padding: 10px;
            background: #007BFF;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background: #0056b3;
        }
        .captcha {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 10px;
        }
        .captcha img {
            margin-left: 10px;
            border: 1px solid #ccc;
            width: 100px; /* 调整验证码图片宽度 */
        }
    </style>
</head>
<body>
    <h1>通用中间件系统</h1>
    <div class="container">
        <h2>Login</h2>
        <form id="loginForm">
            <label for="username">Username:</label>
            <input type="text" id="username" name="username" required><br>
            <label for="password">Password:</label>
            <input type="password" id="password" name="password" required><br>
            <div class="captcha">
                <label for="captcha">Captcha:</label>
                <input type="text" id="captcha" name="captcha" required>
                <img id="captchaImage" src="" alt="Captcha">
            </div>
            <div class="remember-me">
                <input type="checkbox" id="rememberMe" name="rememberMe">
                <label for="rememberMe">Remember Me</label>
            </div>
            <button type="button" onclick="submitLogin()">Login</button>
        </form>
    </div>
    <script>
        function generateCaptcha() {
            const captchaText = Math.random().toString(36).substring(2, 8);
            document.getElementById('captchaImage').src = 'data:image/svg+xml;base64,' + btoa(`<svg xmlns="http://www.w3.org/2000/svg" width="100" height="40"><rect width="100" height="40" fill="white"/><text x="10" y="25" font-size="20" fill="black">${captchaText}</text></svg>`);
            return captchaText;
        }

        let currentCaptcha = generateCaptcha();

        function submitLogin() {
            const formData = new FormData(document.getElementById('loginForm'));
            const loginData = {};
            formData.forEach((value, key) => {
                if (key !== 'rememberMe') {
                    loginData[key] = value;
                }
            });

            if (loginData.captcha !== currentCaptcha) {
                alert('Invalid captcha');
                currentCaptcha = generateCaptcha();
                return;
            }

            if (formData.get('rememberMe')) {
                localStorage.setItem('username', loginData.username);
                localStorage.setItem('password', loginData.password);
            }

            fetch('/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(loginData)
            }).then(response => {
                if (response.ok) {
                    window.location.href = '/static/index.html';
                } else {
                    alert('Invalid credentials');
                }
            });
        }

        document.addEventListener('DOMContentLoaded', (event) => {
            const savedUsername = localStorage.getItem('username');
            const savedPassword = localStorage.getItem('password');
            if (savedUsername && savedPassword) {
                document.getElementById('username').value = savedUsername;
                document.getElementById('password').value = savedPassword;
                document.getElementById('rememberMe').checked = true;
            }
        });
    </script>
</body>
</html>
